package com.maxd.service.helpTask;

import com.maxd.model.HelpSendOrder;
import com.maxd.model.HelpSendOrderDetails;
import com.maxd.model.HelpTask;
import com.maxd.model.TimedTask;
import com.maxd.respository.helpTask.HelpSendOrderDetailsJpaRepository;
import com.maxd.respository.helpTask.HelpSendOrderDetailsRepository;
import com.maxd.respository.helpTask.HelpSendOrderRepository;
import com.maxd.respository.timedTask.TimedTaskJpaRepository;
import com.maxd.respository.timedTask.TimedTaskRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 接单详细信息
 */
@Service
public class HelpSendOrderDetailsServiceImpl implements HelpSendOrderDetailsService {

    /** 接单详细信息 */
    @Autowired
    private HelpSendOrderDetailsJpaRepository helpSendOrderDetailsJpaRepository;
    /** 接单详细信息自定义方法 */
    @Autowired
    private HelpSendOrderDetailsRepository helpSendOrderDetailsRepository;
    /** 接单详细信息自定义方法 */
    @Autowired
    private HelpSendOrderRepository helpSendOrderRepository;
    /** 定时任务 */
    @Autowired
    private TimedTaskJpaRepository timedTaskJpaRepository;
    @Autowired
    private TimedTaskRepository timedTaskRepository;

    @Override
    public List<HelpSendOrderDetails> selectByHelpSendOrderId(Long helpTakeOrderId) {
        return helpSendOrderDetailsRepository.selectByHelpSendOrderId(helpTakeOrderId);
    }

    @Override
    @Transactional
    public int saveAll(List<HelpSendOrderDetails> helpTakeOrderList, HelpTask helpTask,HelpSendOrder helpSendOrder) {
        for(HelpSendOrderDetails helpSendOrderDetails:helpTakeOrderList){
            helpSendOrderDetails.setHelpSendOrderId(helpSendOrder.getId());
        }
        helpSendOrderDetailsJpaRepository.saveAll(helpTakeOrderList);
        //提交活动 删除另外一个定时任务
        timedTaskRepository.deleteByObjectId(helpSendOrder.getId(),2);
        //添加定时任务
        TimedTask timedTask=new TimedTask();
        timedTask.setType(3);
        timedTask.setObjectId(helpSendOrder.getId());
        timedTask.setTitle("助力待审核任务定时");
        timedTask.setCreateTime(new Date());
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MINUTE,helpTask.getAuditTime());
        timedTask.setEndTime(cal.getTime());
        timedTaskJpaRepository.save(timedTask);
        helpSendOrderRepository.updateHelpSendOrder(helpSendOrder.getId(),1,helpSendOrder.getContent());
        return 1;
    }

}
